//
//  AppDelegate.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/8.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        
        setupApperance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        let tabbar = HMMainViewController()
//        tabbar.view.backgroundColor = UIColor.redColor()
//       
        
////      首页
//        let homeVc = HMHomeTableViewController()
//         homeVc.title = "首页"
//
//        homeVc.tabBarItem.image = UIImage(named: "tabbar_home")
//        homeVc.tabBarItem.selectedImage = UIImage(named: "tabbar_home_selected")
//        tabbar.addChildViewController(UINavigationController(rootViewController : homeVc))
////  消息
//        let message = HMMessageTableViewController()
//        message.title = "信息"
//        message.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        tabbar.addChildViewController(UINavigationController (rootViewController: message))
//    
////  发现
//        let discover = HMDiscoverTableViewController()
//        discover.title = "发现"
//        discover.tabBarItem.image = UIImage(named: "tabbar_discover")
//        tabbar.addChildViewController(UINavigationController.init(rootViewController: discover))
////  我
//        let profile = HMProfileTableViewController()
//        profile.title = "我"
//        profile.tabBarItem.image = UIImage(named: "tabbar_profile")
//        tabbar.addChildViewController(UINavigationController.init(rootViewController: profile))
        
        
//        HMPrint("isNewVersion: \(isNewVersion())")

        HMPrint("用户是否登陆\(HMUserAccountViewModel.sharedInstance.userLogin)")
        
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = HMWelcomeViewController()

        window?.makeKeyAndVisible()
        
        return true
    }
    
//    提供一个方法切换控制器都来这里
    func switchViewController(controller : UIViewController) {
        window?.rootViewController = controller
    }
    
//    进入程序设置默认控制器
    private func defaultViewController() ->UIViewController{
    
        if !HMUserAccountViewModel.sharedInstance.userLogin {
            
//            如果没有登陆
            return HMMainViewController()
        }
        
//        标示已经登陆
        let newVersion = isNewVersion()
        
        return newVersion ? HMNewFeatureViewController() : HMWelcomeViewController()
        
    
    }
    
    
//设置uibarbuttonitem颜色
    private func setupApperance() {
    
        let barbuttonApperance = UIBarButtonItem.appearance()
        barbuttonApperance.tintColor = UIColor.orangeColor()
        
    
    }
    
    
//    判断新版本
    private func isNewVersion() -> Bool{
    
//       获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
        
//        获取上一次版本号
        let sanboxKey = "sanboxKey"
        let sanboxKeyVersion = NSUserDefaults.standardUserDefaults().stringForKey(sanboxKey)
//        获取新版本号
        let newVersion = (currentVersion != sanboxKeyVersion)
        HMPrint("currentVersion\(currentVersion)///sanboxKeyVersion\(sanboxKeyVersion)")
//        判断新版号，并存储到偏好设置中
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sanboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        
        
    
    
    
    return newVersion
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

